package com.jiaquyun.jcyx.main.goods.adapter

import android.os.Bundle
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import androidx.constraintlayout.motion.widget.MotionLayout
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.CartGoods
import com.jiaquyun.jcyx.entity.GoodsGroup
import com.jiaquyun.jcyx.main.goods.GoodsDetailsActivity
import com.jiaquyun.jcyx.utils.SpanUtils
import com.module.lemlin.base.RecyclerViewHolder
import com.module.lemlin.image.loadCropImage
import com.module.lemlin.image.loadRoundImage
import com.youth.banner.adapter.BannerAdapter

class GoodsListAdapter : BaseQuickAdapter<GoodsGroup, BaseViewHolder>(R.layout.item_goods_list),
    LoadMoreModule {

    init {
        addChildClickViewIds(R.id.ivGoodsPlus, R.id.ivGoodsMinus)

        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            val bundle = Bundle().apply {
                putInt(GoodsDetailsActivity.KEY_GOODS_ID, itemData.id)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GoodsGroup) {
        holder.setText(R.id.tvGoodsName, item.goods_name)
            .setText(R.id.tvGoodsPrice, SpanUtils.setMoneySymbol(item.guidance_price, item.price))
            .setText(R.id.tvGoodsNum, item.cart_num.toString())
            .setGone(R.id.flGoodsDim, (item.shop_count != 0))
            .getView<ImageView>(R.id.ivGoodsIcon)
            .loadRoundImage(item.image, radius = ConvertUtils.dp2px(5f))

        val motionLayout: MotionLayout = holder.getView(R.id.motionLayout)
        if (item.cart_num == 0) {
            motionLayout.transitionToStart()
        } else {
            motionLayout.transitionToEnd()
        }

    }

    fun setData(position: Int, goodsNum: Int = 0, cartId: Int = 0) {
        this.setData(position, data[position].apply {
            cart_id = if (cartId == 0) cart_id else cartId
            cart_num = goodsNum
        })
    }
}


class GoodsHomeAdapter : BaseQuickAdapter<GoodsGroup, BaseViewHolder>(R.layout.item_goods_home) {

    init {
        addChildClickViewIds(R.id.ivGoodsPlus, R.id.ivGoodsMinus)

        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            val bundle = Bundle().apply {
                putInt(GoodsDetailsActivity.KEY_GOODS_ID, itemData.id)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GoodsGroup) {
        holder.setText(R.id.tvGoodsName, item.goods_name)
            .setText(R.id.tvGoodsPrice, SpanUtils.setMoneySymbol(item.guidance_price, item.price))
            .setText(R.id.tvGoodsNum, item.cart_num.toString())
            .getView<ImageView>(R.id.ivGoodsIcon)
            .loadCropImage(item.image)

        val motionLayout: MotionLayout = holder.getView(R.id.motionLayout)

        if (item.cart_num == 0) {
            motionLayout.transitionToStart()
        } else {
            motionLayout.transitionToEnd()
        }
    }

    fun setData(position: Int, goodsNum: Int = 0, cartId: Int = 0) {
        this.setData(position, data[position].apply {
            cart_id = if (cartId == 0) cart_id else cartId
            cart_num = goodsNum
        })
    }
}

class GoodsCartAdapter : BaseQuickAdapter<CartGoods, BaseViewHolder>(R.layout.item_goods_cart) {
    init {
        addChildClickViewIds(
            R.id.cbGoodsCartOpt,
            R.id.flGoodsCartReduce,
            R.id.flGoodsCartPlus,
            R.id.ivGoodsCartDelete
        )
        //view.animate().scaleX(1f).translationX(ConvertUtils.dp2px(35.5f).toFloat()).start()
        //view.animate().scaleX(0f).translationX(ConvertUtils.dp2px(35.5f).toFloat()).start()
    }

    override fun convert(holder: BaseViewHolder, item: CartGoods) {
        holder.setText(R.id.tvGoodsCartName, item.goods_name)
            .setText(R.id.tvGoodsCartCount, item.goods_num.toString())
            .setText(
                R.id.tvGoodsCartPrice, SpanUtils.setMoneySymbol(item.goods_price, "0")
            )

        holder.getView<ImageView>(R.id.ivGoodsCartPic)
            .loadRoundImage(item.image)
        holder.getView<CheckBox>(R.id.cbGoodsCartOpt).isChecked = item.isSelected

    }
}


class GoodsDetailsImageAdapter :
    BannerAdapter<String, RecyclerViewHolder>(mutableListOf()) {
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val imageView = ImageView(parent.context)
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return RecyclerViewHolder(imageView)
    }

    override fun onBindView(holder: RecyclerViewHolder, data: String, position: Int, size: Int) {
        (holder.itemView as ImageView).loadRoundImage(data, radius = 15)
    }
}